package org.adream.account.service;

import org.adream.account.aop.annotation.DataSource;
import org.adream.account.dao.Dream2020DonornDreamCenterDao;
import org.adream.account.dao.Dream2020DreamCenterDao;
import org.adream.account.entity.Dream2020DonornDreamCenterLinkEntity;
import org.adream.account.entity.Dream2020DreamCenterEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.util.Constant;
import org.adream.account.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@DataSource("adreambox2020DataSource")
public class Dream2020DreamCenterService {
	
	@Autowired
	private Dream2020DreamCenterDao dream2020DreamCenterDao;
	
	@Autowired
	private Dream2020DonornDreamCenterDao dream2020DonornDreamCenterDao;
	
	private final static Logger logger = LoggerFactory.getLogger(Dream2020DreamCenterService.class);
	
	/**
	 * 新增或更新新版盒子梦想中心
	 * @param dream2020DreamCenterEntity
	 * @return
	 */
	public ResultModel<?> addOrUpdateDream2020DreamCenter(Dream2020DreamCenterEntity dream2020DreamCenterEntity) {
		Dream2020DreamCenterEntity dream2020DreamCenter = dream2020DreamCenterDao.queryDream2020DreamCenterById(dream2020DreamCenterEntity.getId());
		if(dream2020DreamCenter == null) {
			int addDream2020DreamCenterResult = dream2020DreamCenterDao.addDream2020DreamCenter(dream2020DreamCenterEntity);
			if(addDream2020DreamCenterResult == Constant.ZERO) {
				logger.warn("新增新版盒子梦想中心失败,数据库错误:(");
				return new ResultModel<String>(ResultModel.ERR_DATABASE, "系统错误dc100,请联系系统管理员", null);
			}
			return new ResultModel<String>(ResultModel.SUCCESS, "新增新版盒子梦想中心成功", null);
		}
		Utils.copyPropertiesIgnoreNull(dream2020DreamCenterEntity, dream2020DreamCenter);
		dream2020DreamCenterDao.updateDream2020DreamCenter(dream2020DreamCenter);
		return new ResultModel<String>(ResultModel.SUCCESS, "更新新版盒子梦想中心成功", null);
	}

	/**
	 * 新增或更新新版盒子捐赠方-梦想中心
	 * @param dream2020DonornDreamCenterLinkEntity
	 * @return
	 */
	public ResultModel<?> addOrUpdateDream2020DonornDreamCenter(Dream2020DonornDreamCenterLinkEntity dream2020DonornDreamCenterLinkEntity) {
		Dream2020DonornDreamCenterLinkEntity dream2020DonornDreamCenter = dream2020DonornDreamCenterDao.queryDream2020DonornDreamCenterById(
				dream2020DonornDreamCenterLinkEntity.getDonornId(), dream2020DonornDreamCenterLinkEntity.getDcid());
		if(dream2020DonornDreamCenter == null) {
			int addDream2020DonornDreamCenterResult = dream2020DonornDreamCenterDao.addDream2020DonornDreamCenter(dream2020DonornDreamCenterLinkEntity);
			if(addDream2020DonornDreamCenterResult == Constant.ZERO) {
				logger.warn("新增新版盒子捐赠方-梦想中心失败,数据库错误:(");
				return new ResultModel<String>(ResultModel.ERR_DATABASE, "系统错误ddc002,请联系系统管理员", null);
			}
			return new ResultModel<String>(ResultModel.SUCCESS, "新增新版盒子捐赠方-梦想中心成功", null);
		}
		dream2020DonornDreamCenter.setDeleteTime(dream2020DonornDreamCenterLinkEntity.getDeleteTime());
		dream2020DonornDreamCenterDao.updateDream2020DonornDreamCenter(dream2020DonornDreamCenter);
		return new ResultModel<String>(ResultModel.SUCCESS, "更新新版盒子捐赠方-梦想中心成功", null);
	}
}